package br.com.tiespec.web.Util;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfDescricao extends PdfPageEventHelper{
	
	private String tituloRelatorio;
	private String nomeCurtoUsuarioLogado;
	private String dataHoraRelatorio;
	private Font fonteBranca = FontFactory.getFont("Arial", 12, Font.BOLD, new BaseColor(255,255,255));
	private Font fonteHeader = FontFactory.getFont("Arial", 16, Font.BOLD, new BaseColor(0, 0, 0));
	private Font fonteTitle = FontFactory.getFont("Arial", 24, Font.BOLDITALIC, new BaseColor(29, 17, 108));
	private Font fonteFooter = FontFactory.getFont("Arial", 8, Font.BOLD, new BaseColor(128, 128, 128));
	private PdfPCell logomarca;
	
	public PdfDescricao(String usuario) {
		super();
		nomeCurtoUsuarioLogado = usuario;
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
		dataHoraRelatorio = formatter.format(new Date());
		logomarca = logo();
	}

	public void onStartPage(PdfWriter writer, Document doc){
        PdfPTable header = new PdfPTable(100);
        header.setTotalWidth(doc.getPageSize().getWidth());
        
        PdfPCell titulo = new PdfPCell(new Paragraph(tituloRelatorio, fonteHeader));
        titulo.setBackgroundColor(new BaseColor(237, 237, 254));
    	titulo.setHorizontalAlignment(Element.ALIGN_RIGHT);
    	titulo.setVerticalAlignment(Element.ALIGN_BOTTOM);
    	titulo.setColspan(65);
    	titulo.setPaddingTop(15f);
    	titulo.setPaddingRight(7f);
    	titulo.setPaddingBottom(10f);
    	titulo.setBorder(0);
    	
    	PdfPCell paginacao = new PdfPCell(new Paragraph("" + doc.getPageNumber(), fonteBranca));
    	paginacao.setColspan(5);
    	paginacao.setHorizontalAlignment(Element.ALIGN_CENTER);
    	paginacao.setVerticalAlignment(Element.ALIGN_MIDDLE);
    	paginacao.setBackgroundColor(new BaseColor(29, 17, 108));
    	paginacao.setBorder(0);
    	
    	header.addCell(espacamento(5));
    	header.addCell(logomarca);	
    	header.addCell(titulo);
    	header.addCell(paginacao);
    	header.addCell(espacamento(5));
    	header.addCell(espacamento(5));
    	header.addCell(linha());
    	header.addCell(espacamento(5));
    	
    	header.writeSelectedRows(0,-1, 0, (doc.getPageSize().getHeight()-10), writer.getDirectContent());
   }
	
    public void onEndPage(PdfWriter writer, Document doc){
    	PdfPTable footerTbl = new PdfPTable(100);
        footerTbl.setTotalWidth(doc.getPageSize().getWidth());

        PdfPCell cellNomeCurtoUsuarioLogado = new PdfPCell(new Paragraph(nomeCurtoUsuarioLogado, fonteFooter));
        cellNomeCurtoUsuarioLogado.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellNomeCurtoUsuarioLogado.setBorder(0);
        cellNomeCurtoUsuarioLogado.setColspan(30);
        
        PdfPCell cellPagina = new PdfPCell(new Paragraph("" + doc.getPageNumber() , fonteFooter));
        cellPagina.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellPagina.setColspan(30);
        cellPagina.setBorder(0);
        
        PdfPCell cellDataHoraRelatorio = new PdfPCell(new Paragraph(dataHoraRelatorio, fonteFooter));
        cellDataHoraRelatorio.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cellDataHoraRelatorio.setColspan(30);
        cellDataHoraRelatorio.setBorder(0);
        
        footerTbl.addCell(espacamento(5));
        footerTbl.addCell(linha());
        footerTbl.addCell(espacamento(5));
        footerTbl.addCell(espacamento(5));
        footerTbl.addCell(cellNomeCurtoUsuarioLogado);
        footerTbl.addCell(cellPagina);
        footerTbl.addCell(cellDataHoraRelatorio);
        footerTbl.addCell(espacamento(5));

        footerTbl.writeSelectedRows(0,-1, 0, (doc.bottomMargin() + 10),  writer.getDirectContent());
    }

    	
    public PdfPCell espacamento(Integer espaco){
    	PdfPCell cell = new PdfPCell();
    	cell.setColspan(espaco);
    	cell.setBorder(0);
    	return cell;
    }
    
    private PdfPCell linha(){
    	PdfPCell linha = new PdfPCell(new Paragraph(new Chunk("")));
    	linha.setColspan(90);
    	linha.setHorizontalAlignment(Element.ALIGN_CENTER);
    	linha.setVerticalAlignment(Element.ALIGN_TOP);
    	linha.setBorder(0);
    	linha.setBorderWidthTop(0.3f);
    	return linha;
    }
    
    private PdfPCell logo(){
    	PdfPCell logo = new PdfPCell(new Paragraph("TiEspec", fonteTitle));
    	logo.setBackgroundColor(new BaseColor(237, 237, 254));
    	logo.setHorizontalAlignment(Element.ALIGN_LEFT);
    	logo.setBorder(0);
    	logo.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    	logo.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
    	logo.setColspan(20);
    	logo.setPaddingTop(6f);
    	logo.setPaddingLeft(5f);
    	try {
//    		System.out.println(dirImg);
//    		Image img = Image.getInstance(dirImg);
//    		img.scalePercent(90);
//    		logo = new PdfPCell(img);
//    		logo.setHorizontalAlignment(Element.ALIGN_LEFT);
//        	logo.setBorder(0);
//        	logo.setColspan(20);
    	} catch (Exception e) {
    		System.out.println("Erro ao carregar a imagem no PDF: " + e.getCause());
    		e.printStackTrace();
    	}
    	return logo;
    }
    
	public String getTituloRelatorio() {
		return tituloRelatorio;
	}

	public void setTituloRelatorio(String tituloRelatorio) {
		this.tituloRelatorio = tituloRelatorio;
	}
}